package br.edu.ufcg.lsd.wbis.control.user;

import javax.persistence.Entity;
import javax.persistence.Id;

import br.edu.ufcg.lsd.wbis.control.util.SecurityUtil;

/**
 * This class represents a system user
 * 
 * @author Paulo Ricardo
 */
@Entity
public class User {

	private String name;

	private String email;

	@Id
	private String login;

	private String password;

	private UserLevel level;

	private boolean authorized;

	public User() {
		// Do nothing!
	}

	/**
	 * 
	 * @param name
	 * @param email
	 * @param login
	 * @param password
	 * @param level
	 */
	public User(String name, String email, String login, String password, boolean authorized, UserLevel level) {
		this(name, email, login, password, level, level.equals(UserLevel.ADMIN) ? true : authorized);
	}

	/**
	 * 
	 * @param name
	 * @param email
	 * @param login
	 * @param password
	 * @param level
	 * @param authorized
	 */
	public User(String name, String email, String login, String password, UserLevel level, boolean authorized) {
		this.name = name;
		this.email = email;
		this.login = login;
		this.password = password;
		this.level = level;
		this.authorized = authorized;
	}

	/**
	 * 
	 * @return name
	 */
	public String getName() {
		return name;
	}

	/**
	 * 
	 * @param name
	 */
	protected void setName(String name) {
		this.name = name;
	}

	/**
	 * 
	 * @return email
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * 
	 * @param mail
	 */
	protected void setEmail(String mail) {
		email = mail;
	}

	/**
	 * 
	 * @return login
	 */
	@Id
	public String getLogin() {
		return login;
	}

	/**
	 * 
	 * @param login
	 */
	protected void setLogin(String login) {
		this.login = login;
	}

	/**
	 * 
	 * @return pssword
	 */
	private String getPassword() {
		return password;
	}

	/**
	 * 
	 * @param password
	 */
	protected void setPassword(String password) {
		this.password = password;
	}

	/**
	 * 
	 * @return level
	 */
	public UserLevel getLevel() {
		return level;
	}

	/**
	 * 
	 * @param level
	 */
	protected void setLevel(UserLevel level) {
		this.level = level;
	}

	/**
	 * 
	 * @param password
	 * @return valid
	 */
	protected boolean isValid(String password) {
		return getPassword().equals(password);
	}

	/**
	 * 
	 * @return authorized
	 */
	public boolean isAuthorized() {
		return authorized;
	}

	/**
	 * 
	 * @param authorized
	 */
	protected void setAuthorized(boolean authorized) {
		this.authorized = authorized;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (authorized ? 1231 : 1237);
		result = prime * result + ((email == null) ? 0 : email.hashCode());
		result = prime * result + ((level == null) ? 0 : level.hashCode());
		result = prime * result + ((login == null) ? 0 : login.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((password == null) ? 0 : password.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		User other = (User) obj;
		if (authorized != other.authorized)
			return false;
		if (email == null) {
			if (other.email != null)
				return false;
		} else if (!email.equals(other.email))
			return false;
		if (level == null) {
			if (other.level != null)
				return false;
		} else if (!level.equals(other.level))
			return false;
		if (login == null) {
			if (other.login != null)
				return false;
		} else if (!login.equals(other.login))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (password == null) {
			if (other.password != null)
				return false;
		} else if (!password.equals(other.password))
			return false;
		return true;
	}

	public boolean checkPassword(String password) {
		String pswdHash = SecurityUtil.generateHash(password);
		return this.getPassword().equals(pswdHash);
	}
	
	@Override
	public String toString() {
		StringBuilder bld = new StringBuilder();
		bld.append("Login: ");
		bld.append(this.getLogin());
		bld.append("\n");
		bld.append("Password: ");
		bld.append(this.getPassword());
		bld.append("\n");
		bld.append("Name: ");
		bld.append(this.getName());
		bld.append("\n");
		bld.append("Email: ");
		bld.append(this.getEmail());
		bld.append("\n");
		bld.append("Level: ");
		bld.append(this.getLevel());
		bld.append("\n");
		bld.append("Authorized: ");
		bld.append(this.isAuthorized());
		bld.append("\n");
		return bld.toString();
	}

}
